var yangdate = require("sign_in_ss.js");
// pages/sign_in/sign_in.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      signDay:[{"signDay":"9"},{"signDay":"11"},{"signDay":"12"},{"signDay":"15"}],
      signs:[1,2,3,5,6,7],
      signtype:"1",
      signDays:[],
      todayDate:"1",
      todayMonth:"",
      todayYear:"",
      nextMonth:"",
      nextYear:"",
      prevYear:"",
      prevMonth:"",
      seriesCount:"",
      series_gos:"7",
      for_signs:"none",
      powerData:"0",
      sign:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

      var getToday = new Date();
      var todayDate =getToday.getDate();
      var todayMonths =getToday.getMonth();
      var todayMonth=(todayMonths+1);
      var todayYear =getToday.getFullYear();
      var todayss =getToday.getDate();
      if(todayMonth<10){
             var todayMonthss="0"+todayMonth;
         }else{
             var todayMonthss=todayMonth;
         }
    console.log("todayss:"+todayss);
      var godates = todayYear + "-" + todayMonthss + "-01";
      var that =this;
    console.log("godates:" + godates);
    wx.getStorageSync("Openid:" + wx.getStorageSync("OpenId"));
    wx.request({
      url: "https://www.brvpmusic.com/sign_space/parameter",
      data: {
        'cOpenid': wx.getStorageSync("OpenId"),
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log("1:" + res.data.body);
        var continuity = res.data.body;
        console.log('返回成功:' + continuity);
        wx.request({
          url: "https://www.brvpmusic.com/sign_space/list",
          data: {
            'cOpenid': wx.getStorageSync("OpenId"),
            'godates': godates
          },
          method: 'GET',
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            console.log("1:" + res.data.body);
            console.log('返回成功 continuity:=' + continuity);
            var result = res.data.body;
            // console.log(typeof (a));
            // console.log("a:"+a);
            var data = { seriesCount: continuity, signDays: result };
            console.log("data:" + data);
            var $datas = data;
            var signDate_arr = new Array();
            var anns = $datas.signDays;
            var count_signday = $datas.seriesCount;
            console.log("结果:------" + count_signday);
            if (count_signday > 7) {
              var series_gos = "0";
              console.log("series_gos1:" + series_gos);
            } else if (count_signday < 0) {
              var series_gos = 99;
              console.log("series_gos2:" + series_gos);

            } else {
              var series_gos = 7 - parseInt(count_signday);
              console.log("series_gos3:"+series_gos);
            }
            console.log("结果1：--" + count_signday);
            console.log("结果2：--" + series_gos);
            
            that.setData({
              seriesCount: count_signday,
              series_gos: series_gos,
            });
            for (var p in anns) {//遍历json对象的每个key/value对,p为key
              var newdats = anns[p];
              signDate_arr.push(newdats);
            }
            if (signDate_arr.indexOf(todayss) > -1) {
              console.log("当前已签到");
              that.setData({
                signtype: "2",
              });
            } else {
              console.log("当前未签到");
              that.setData({
                signtype: "1",
              });
            }
            console.log(signDate_arr[0]);
            yangdate.yang_date.bulidCal(todayYear, todayMonth, that, signDate_arr);
            //初始化加载日历



          },
          fail: function (res) {
            console.log('返回失败');
            wx.showToast({
              title: '未签到不能查看',
              icon: 'loading',
              duration: 1500
            });
          }

        });

        // console.log(typeof (a));
        // console.log("a:"+a);
      },
      fail: function (res) {
        console.log('返回失败');
        wx.showToast({
          title: '未签到不能查看',
          icon: 'loading',
          duration: 1500
        });
      }

    });

    
    
    
    this.setData({
      todayDate: todayDate,
      todayMonth: todayMonth,
      todayYear: todayYear,
      prevYear: todayYear,
      nextYear: todayYear,
      prevMonth: todayMonth,
      nextMonth: todayMonth,
      showYear: todayYear,
      showMonth: todayMonth,
    });

   
                  
  },
    sign_start: function () {
      var that=this;
      wx.request({
        url: "https://www.brvpmusic.com/sign_space/sign_in",
        data: {
          'cOpenid': wx.getStorageSync("OpenId")
        },
        method: 'GET',
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(res.data);
          that.setData({
            'sign':res.data.body
          })
          var powerData = 100;
          if (res.data.status == true) {
            wx.showToast({
              title: '签到成功',
              icon: 'success',
              duration: 1500
            });}
            else{
            wx.showToast({
              title: '您已签到',
              icon: 'success',
              duration: 1500
            });
            }
          
        },
        fail: function (res) {
          console.log('返回失败');
          wx.showToast({
            title: '系统错误',
            icon: 'loading',
            duration: 1500
          });
        }

      });


      this.onLoad();
                   
                    //签到成功后重新调用后台接口加载新的签到数据
                    this.setData({
                        for_signs:"block",
                        signtype:"2",
                        // powerData:powerData,
                    });
                   
    },

    close_qdbox: function () {
      var seriesCount = this.data.seriesCount;
      console.log("seriesCount===" + seriesCount);
      // var seriesCount = seriesCount+1;
      if(seriesCount<7){
          var series_gos=7-seriesCount;
        console.log("111111111")
          
      }else{
          var series_gos="0";
          console.log("22222222222222")
      }
        this.setData({
            seriesCount:seriesCount,
            series_gos:series_gos,
            for_signs:"none",
        });
    },
    sign_end: function () {
        wx.showToast({
            title: '今日已经签到',
            icon: 'loading',
            duration: 1500
        });
    },
    sign_prev: function () {
        console.log("上一月");
        var showMonth=this.data.showMonth;
        var todayMonth =this.data.todayMonth;
        if(showMonth=="1"){
            var showMonth="12";
            var showYear=parseInt(this.data.showYear)-1;
        }else{
            var showMonth=parseInt(this.data.showMonth)-1;
            var showYear=this.data.showYear;
        }
        if(parseInt(todayMonth-3)==showMonth){
            wx.showToast({
                title: '不能查看更多了',
                icon: 'loading',
                duration: 1500
            });
            return;
        }
        var that =this;
        if(showMonth<10){
            var showMonths="0"+showMonth;
        }else{
            var showMonths=todayMonth;
        }
        var godates = showYear + "-" + showMonths + "-01";
      console.log("godates:"+godates);
      wx.request({
        url: "https://www.brvpmusic.com/sign_space/list",
        data: {
          'cOpenid': wx.getStorageSync("OpenId"),
          'godates': godates
        },
        method: 'GET',
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          var result = res.data.body;
          var $datas = { seriesCount: 1, signDays: result};
          var anns = $datas.signDays;
          var signDate_arr = [];
          for (var p in anns) {//遍历json对象的每个key/value对,p为key
            var newdats = anns[p];
            signDate_arr.push(newdats);
          }
          console.log(signDate_arr[0]);
          yangdate.yang_date.bulidCal(showYear, showMonth, that, signDate_arr);
        },
        fail: function (res) {
           console.log('返回失败');
            wx.showToast({
            title: '未签到不能查看',
            icon: 'loading',
            duration: 1500
          });
        }


      });


                  
         //初始化加载日历
        this.setData({
            showYear:showYear,
            showMonth:showMonth,
        });
    },
    sign_next: function () {
        console.log("下一月");
        var showMonth=this.data.showMonth;
        var todayMonth =this.data.todayMonth;
        if(todayMonth==showMonth){
            wx.showToast({
                title: '未签到不能查看',
                icon: 'loading',
                duration: 1500
            });
            return;
        }
        if(showMonth=="12"){
            var showMonth="1";
            var showYear=parseInt(this.data.showYear)+1;
        }else{
            var showMonth=parseInt(this.data.showMonth)+1;
            var showYear=this.data.showYear;
        }


        var that =this;

        if(showMonth<10){
            var showMonths="0"+showMonth;
        }else{
            var showMonths=todayMonth;
        }
        var godates = showYear + "-" + showMonths + "-01";
      console.log("godates:" + godates);
      wx.request({
        url: "https://www.brvpmusic.com/sign_space/list",
        data: {
          'cOpenid': wx.getStorageSync("OpenId"),
          'godates': godates
        },
        method: 'GET',
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          var result = res.data.body;

          var $datas = { seriesCount: 1, signDays: result };
          var signDate_arr = new Array();
          var anns = $datas.signDays;
          for (var p in anns) {//遍历json对象的每个key/value对,p为key
            var newdats = anns[p];
            signDate_arr.push(newdats);
          }
          console.log(signDate_arr[0]);
          yangdate.yang_date.bulidCal(showYear, showMonth, that, signDate_arr);
        },
        fail: function (res) {
          console.log('返回失败');
          wx.showToast({
            title: '未签到不能查看',
            icon: 'loading',
            duration: 1500
          });
        }


      });


                    //初始化加载日历


        this.setData({
            showYear:showYear,
            showMonth:showMonth,
        });

    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})